package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.model.Food;
import com.model.FoodType;
import com.service.FoodService;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin("*")
public class FoodController {
    @Autowired
    private FoodService foodService;

    /**
     * 添加食品接口
     */
    @PostMapping("/foods")
    public Result addFood(@RequestBody Food food) {
        food.setCreateTime(new Date());
        food.setUpdateTime(new Date());
        boolean save = foodService.save(food);
        if (save){
            return new Result("200","添加成功",1,null);
        } else {
            return new Result("500","添加失败,请联系管理员询问原因",0,null);
        }
    }

    /**
     * 根据ID删除食品接口
     */
    @DeleteMapping("/foods/{id}")
    public Result deleteFoodById(@PathVariable Integer id) {
        Boolean res = foodService.removeById(id);
        if (res){
            return new Result("200","删除成功",1,null);
        } else {
            return new Result("500","删除失败",0,null);
        }
    }

    /**
     * 根据食品名 查询食品
     * @param foodName 食品名
     * @return 返回查询到的食品对象
     */
    @RequestMapping(value="/foods/{foodName}", method=RequestMethod.GET, params="foodName")
    public Result getFoodByFoodName(@PathVariable("foodName") String foodName) {
        Food food = foodService.getOne(new QueryWrapper<Food>().eq("food_name", foodName));
        return new Result("200","查询成功",1, List.of(food));
    }

    /**
     * 根据食品类型 查询食品
     * @param type 食品类型名
     * @return 返回查询到的食品对象集合
     */
    @RequestMapping(value="/foods/{type}", method=RequestMethod.GET, params="type")
    public Result getFoodsByType(@PathVariable("type") String type) {
        List<Food> foodList = foodService.list(new QueryWrapper<Food>().eq("type", type));
        return new Result("200","查询成功",foodList.size(), List.of(foodList));
    }

    /**
     * 查询单个食品接口
     */
    /*@GetMapping("/foods/{idOrNameOrStarOrType}")
    public Result getFoodByIdOrNameOrStarOrType(@PathVariable String idOrNameOrStarOrType) {
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.and(i -> i.eq("id", idOrNameOrStarOrType)
                .or()
                .like("food_name", idOrNameOrStarOrType)
                .or()
                .eq("star", idOrNameOrStarOrType)
                .or()
                .eq("type",idOrNameOrStarOrType));
        Food food = foodService.getOne(wrapper);
        return new Result("200","查询成功",1, List.of(food));
    }*/

    /**
     * 查询全部食品接口
     */
    @GetMapping("/foods")
    public Result getAllFoods() {
        List<Food> list = foodService.list();
        return new Result("200","查询成功",list.size(),list);
    }

    /**
     * 分页查询食品接口
     */
    @GetMapping("/foods/page/{pageNum}/{pageSize}")
    public IPage<Food> getFoodsByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        Page<Food> page = new Page<>(pageNum, pageSize);
        return foodService.selectPage(page, null);
    }

    /**
     * 根据ID更新食品接口
     */
    @PutMapping("/foods/{id}")
    public Result updateFoodById(@PathVariable Integer id, @RequestBody Food food) {
        food.setId(id);
        food.setUpdateTime(new Date());
        Boolean res = foodService.updateById(food);
        if (res){
            return new Result("200","更新成功",1,List.of(food));
        } else {
            return new Result("500","更新失败",0,null);
        }
    }
}
